<?php
// No directAcces
defined( '_AppBackMiage' ) or die( 'Restricted access' );
if(isAdministrateur ()){
    include_once (DS."backoffice/include/header.php");

    if(isset($_GET['uid']) && !empty($_GET['uid']))
    {
            $enseignant = new Enseignant($_GET['uid']);

            if(isset($_POST['submit']))
            {
                // variable qui va nous servir pour savoir s'il y a a eu une erreur ;)
                $form = true;
                $errMSG = array();
                if(isset($_POST['lastname']) && !empty($_POST['lastname']))
                {
                    $tmp = $enseignant->setNomUtilisateur($_POST['lastname']);
                    if($tmp['boolOk'] === false)
                    {
                        $errMSG['nom'] = $tmp['errmsg'];
                        $form = false;
                    }
                }
                if(isset($_POST['firstname']) && !empty($_POST['firstname']))
                {
                    $tmp = $enseignant->setPrenomUtilisateur($_POST['firstname']);
                    if($tmp['boolOk'] === false)
                    {
                        $errMSG['prenom'] = $tmp['errmsg'];
                        $form = false;
                    }
                }
                if(isset($_POST['email']) && !empty($_POST['email']))
                {
                    $tmp = $enseignant->setEmailUtilisateur($_POST['email']);
                    if($tmp['boolOk'] === false)
                    {
                        $errMSG['mail'] = $tmp['errmsg'];
                        $form = false;
                    }
                }

                if(isset($_POST['ufr']) && !empty($_POST['ufr']))
                {
                    $tmp = $enseignant->setIdUFR($_POST['ufr']);
                    if($tmp['boolOk'] === false)
                    {
                        $errMSG['ufr'] = $tmp['errmsg'];
                        $form = false;
                    }
                }

                if(isset($_POST['types']) && !empty($_POST['types'] ) )
                {
                        $types = implode(',',$_POST['types']);
                        $tmp = $enseignant->setIdTypeUser($types);
                        if($tmp['boolOk'] === false)
                        {
                                $errMSG['types'] = $tmp['errmsg'];
                                $form = false;
                        }
                }
                else
                {
                    $contact->setIdTypeUser("");
                }

                if($form === true)
                {
                    $enseignant->update();
                    redirectTo(URLBACK."_userenseignant_list");
                }
            }

    }
    else
    {
            // redirection
        redirectTo(URLBACK."_userenseignant_list");
    }
    $lstTypes = TypeUtilisateur::getList();
    $lstUFR = UFR::getList();
    ?>
    <?php

    include_once( DS."backoffice/include/toppanel_utilisateurs.php");

    ?>
    <div id="wrapper">
        <div id="content">
            <div id="box">
                <h3 id="adduser">Edition d'un enseignant</h3>
                <form id="form" method="post">
                <fieldset id="personal">
                    <legend>Informations Personnelles</legend>

                    <label for="lastname">Nom : </label>
                    <input name="lastname" id="lastname" type="text" tabindex="1" value="<?php echo $enseignant->getNomUtilisateur(); ?>" />
                    <span class="error"><?php if(isset($errMSG['nom'])){ echo $errMSG['nom']; } ?></span>
                    <br />
                    <label for="firstname">Prenom : </label>
                    <input name="firstname" id="firstname" type="text" tabindex="2" value="<?php echo $enseignant->getPrenomUtilisateur(); ?>"/>
                    <span class="error"><?php if(isset($errMSG['prenom'])){ echo $errMSG['prenom']; } ?></span>
                    <br />
                    <label for="email">Email : </label>
                    <input name="email" id="email" type="text"tabindex="2" value="<?php echo $enseignant->getEmailUtilisateur(); ?>" />
                    <span class="error"><?php if(isset($errMSG['mail'])){ echo $errMSG['mail']; } ?></span>
                    <br />
                    <p>Envoyer Mot de passe <input name="generatepass" id="yes" type="checkbox"  value="yes" tabindex="35" /></p>
                </fieldset>
                <fieldset id="opt">
                    <legend>OPTIONS</legend>
                    <label for="ufr">UFR : </label>
                    <select name="ufr">
                    <?php
                    if(!empty($lstUFR))
                    {
                        foreach($lstUFR as $id=>$p)
                        {
                            ?>
                            <option value="<?php echo $id; ?>" <?php if($id == $enseignant->getIdUFR()){ echo "selected='selected'"; } ?>>
                                <?php echo $p; ?>
                            </option>
                            <?php
                        }
                    }
                    ?>
                    </select>
                    <span class="error"><?php if(isset($errMSG['promo'])){ echo $errMSG['promo']; } ?></span>
                    <br />
                    <label for="types">Type d'utilisateur : </label>
                    <?php
                    if(!empty($lstTypes))
                    {
                        $typesUser = explode(',',$enseignant->getIdTypeUser());
                        foreach($lstTypes as $type)
                        {
                            ?>
                            <?php echo $type->NomTypeUtilisateur; ?>
                            <input type="checkbox" name="types[]" value="<?php echo $type->IdTypeUtilisateur; ?>" <?php if(in_array($type->IdTypeUtilisateur, $typesUser)){ echo "checked='checked'"; } ?>>
                            &nbsp;|&nbsp;
                            <?php
                        }
                    }
                    ?>
                    <br /><span class="error"><?php if(isset($errMSG['types'])){ echo $errMSG['types']; } ?></span>
                </fieldset>
                <div align="center">
                    <input id="button1" type="submit" name="submit" value="Sauver" />
                    <input id="button2" type="reset" />
                </div>
                </form>
            </div>
            </div>
    <?php
    include_once (DS."backoffice/include/sidebar.php");
    include_once (DS."backoffice/include/footer.php");
}
else
{
    redirectTo(URLBACK);
}
?>